package com.ifcar.app.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import android.util.Log;

import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.service.PushService;
import com.ifcar.app.util.StringUtil;
public class MyApplication extends Application implements OnSharedPreferenceChangeListener{

	private NewsSQLiteOpenHelper sqlOpenHelper;
	
	public void setSqlOpenHelper(NewsSQLiteOpenHelper sqlOpenHelper) {
		this.sqlOpenHelper = sqlOpenHelper;
	}

	private SQLiteDatabase db;
	public void setDb(SQLiteDatabase db) {
		this.db = db;
	}

	private HashMap<String, Object> docIDListMap4Head = new HashMap<String, Object>();
	private HashMap<String, Object> docIDListMap4NonHead = new HashMap<String, Object>();
	private HashMap<String, Object> docIDListMap4Topic = new HashMap<String, Object>();
	
	private List<Map<String, Object>> mVoteList;

	private boolean isExit = true;//判断系统是否退出
	private String lastdocid = "";//用于推送
	private String goHome;
	
	private boolean isCacleOfflineDown = false;
	private boolean isDownOffline = false;
	private boolean isOfflineDownFinish = false;
	private boolean isCustomColumnFinish = false;
	private Intent pushIntent;
	private SharedPreferences set_prefs;
	private ArrayList<Activity> historyRelativeDoc;
	
	// 投票  ====================================
	private boolean keyback = false;
	private String voteId;
	
	public int mHtmlWidth;
	
	public boolean isKeyback() {
		return keyback;
	}

	public void setKeyback(boolean keyback) {
		this.keyback = keyback;
	}
	
	public void setVoteId(String vId) {
		voteId = vId;
	}
	
	public String getVoteId(){
		return voteId;
	}
	// 投票 ====================================
	
	public ArrayList<Activity> getHistoryRelativeDoc() {
	    if(null==historyRelativeDoc){
	        historyRelativeDoc = new ArrayList<Activity>();
	    }
        return historyRelativeDoc;
	}

    public Intent getPushIntent() {
		return pushIntent;
	}

	public static boolean isDownPic = true;
	
	public String getGoHome() {
		return goHome;
	}

	public void setGoHome(String goHome) {
		this.goHome = goHome;
	}
	
	public String getLastdocid() {
		return lastdocid;
	}

	public void setLastdocid(String lastdocid) {
		this.lastdocid = lastdocid;
	}

	public boolean isExit() {
		return isExit;
	}

	public void setExit(boolean isExit) {
		this.isExit = isExit;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		initDB();
		set_prefs = PreferenceManager.getDefaultSharedPreferences(this);
//		boolean autopost = set_prefs.getBoolean("autopost", true);
//		if(autopost){
//			pushIntent = new Intent(this, MsgPushService.class);
//			this.startService(pushIntent);
//		}
		set_prefs.registerOnSharedPreferenceChangeListener(this);
		if (PushService.needPush(this)) {
		    PushService.startPush(this);
		}
	}
	
	@Override
	public void onTerminate() {
		// TODO Auto-generated method stub
		super.onTerminate();
		set_prefs.unregisterOnSharedPreferenceChangeListener(this);
		if(null!=db){
			db.close();
		}
		if(null!=sqlOpenHelper){
			sqlOpenHelper.close();
		}
	}
	
	public void initDB() {
		sqlOpenHelper = new NewsSQLiteOpenHelper(getApplicationContext());
		db = sqlOpenHelper.getWritableDatabase();
	}
	
	public NewsSQLiteOpenHelper getNewsSQLiteOpenHelper() {
		return sqlOpenHelper;
	}
	
	public SQLiteDatabase getSQLiteDatabase() {
		return db;
	}
	
	public HashMap<String, Object> getDocIdListMap(int type) {
		switch(type) {
		case StringUtil.HEAD_TYPE:
			return docIDListMap4Head;
		case StringUtil.NONHEAD_TYPE:
			return docIDListMap4NonHead;
		case StringUtil.TOPIC_TYPE:
			return docIDListMap4Topic;
		}
        return null;
	}
	
	public void setDocIdListMap(int type, String key, Object o, String columnId) {
		switch(type) {
		case StringUtil.HEAD_TYPE:
			docIDListMap4Head.put(key, o);
			break;
		case StringUtil.NONHEAD_TYPE:
			if(docIDListMap4NonHead.get(columnId) == null) {			
				HashMap<String, Object> map = new HashMap<String, Object>();
				docIDListMap4NonHead.put(columnId, map);				
			}
			((HashMap<String, Object>)docIDListMap4NonHead.get(columnId)).put(key, o);
			break;
		case StringUtil.TOPIC_TYPE:
			docIDListMap4Topic.put(key, o);
			break;
		}
		
	}

	public void setVoteList(List<Map<String, Object>> list) {
		mVoteList = list;
	}

	public List<Map<String, Object>> getVoteList() {
		return mVoteList;
	}
	
	public void setIsCancleOfflineDown(boolean status) {
		isCacleOfflineDown = status;
	}
	
	public boolean getIsCancleOfflineDown() {
		return isCacleOfflineDown;
	}
	
	public void setIsDownOffline(boolean status) {
		isDownOffline = status;
	}
	
	public boolean getIsDownOffline() {
		return isDownOffline;
	}
	
	public void setIsOfflineDownFinish(boolean status) {
		isOfflineDownFinish = status;
	}
	
	public boolean getIsOfflineDownFinish() {
		return isOfflineDownFinish;
	}
	
	public void setIsCustomColumnFinish(boolean status) {
		isCustomColumnFinish = status;
	}
	
	public boolean getIsCustomColumnFinish() {
		return isCustomColumnFinish;
	}

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (PushService.KEY_AUTO_POST.equals(key)) {
            if (sharedPreferences.getBoolean(key, true)) {
                System.out.println("start Push for settting");
                PushService.startPush(this);
            } else {
                System.out.println("cancel Push for settting");
                PushService.cancelPush(this);
            }
        }
    }
}
